package com.example.patient.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.common.entity.OrderMain;
import com.example.common.support.ApiException;
import com.example.common.support.Result;
import com.example.common.utils.IdUtils;
import com.example.common.utils.RequestHelp;
import com.example.patient.jpa.OrderMainRepository;
import com.example.patient.properties.WxProp;
import com.example.patient.service.WXPayService;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.net.InetAddress;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author wxy
 * @Date 2021/1/18 9:40
 * @Version 1.0
 */
@RestController
@Api(tags = "微信的支付回调")
@RequestMapping("/Wx/WXPay")
public class WXPayController {
    @Resource
    private WXPayService wxPayService;
    @Resource
    private WxProp wxProp;
    @Resource
    private OrderMainRepository orderMainRepository;

    @ApiOperation(value = "微信支付回调一", notes = "微信支付回调一")
    @PostMapping(value = "/CallBack")
    public Result<Object> CallBack() {
        return Result.success(wxPayService.CallBack());
    }

    @ApiOperation(value = "微信支付回调二", notes = "微信支付回调二")
    @PostMapping(value = "/CallBackWa")
    public Result<Object> CallBackWa() {
        return Result.success(wxPayService.CallBackWa());
    }

    @ApiOperation(value = "微信支付回调测试", notes = "微信支付回调测试")
    @GetMapping(value = "/CallBackTest")
    public Result<Object> CallBackTest(@RequestParam(value = "OrderNo") String orderno,
    @RequestParam(value = "oType") Integer oType) {
        if(oType == 1){
            wxPayService.CallBackTest(orderno);
        }else if(oType == 2){
            OrderMain orderMain = orderMainRepository.findById(orderno).orElse(null);
            WxPayService wxPayService = new WxPayServiceImpl();
            WxPayConfig wxPayConfig = new WxPayConfig();
            wxPayConfig.setAppId(wxProp.getWxAppletsAppId());
            wxPayConfig.setMchId(wxProp.getMchId());
            wxPayConfig.setMchKey(wxProp.getMchKey());
            wxPayConfig.setKeyPath(wxProp.getKeyPath());
            wxPayService.setConfig(wxPayConfig);
            WxPayRefundRequest request = new WxPayRefundRequest();
            request.setOutTradeNo(orderno);
            request.setOutRefundNo("R" + IdUtils.generateId());
            request.setTotalFee(orderMain.getAskMoney().multiply(new BigDecimal(100)).intValue());
            request.setRefundFee(orderMain.getAskMoney().multiply(new BigDecimal(100)).intValue());
            //退款操作
            try {
                wxPayService.refund(request);
            } catch (Exception e) {
                throw new ApiException(1, e.getMessage());
            }
        }

        return Result.success();
    }
}
